package com.example.activiti.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.activiti.engine.IdentityService;
import org.activiti.engine.TaskService;
import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.User;
import org.activiti.engine.impl.persistence.entity.GroupEntity;
import org.activiti.engine.impl.persistence.entity.GroupEntityImpl;
import org.activiti.engine.impl.persistence.entity.UserEntity;
import org.activiti.engine.impl.persistence.entity.UserEntityImpl;
import org.activiti.engine.task.IdentityLink;
import org.activiti.engine.task.Task;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 对用户角色角色进行操作
 * @author zjh
 *
 */
@Service
public class ActivitiUserManageService {
	@Autowired
	private IdentityService identityService;
	@Resource
    private TaskService taskService;
	
	/**
	 * 根据分组获取用户
	 * @return
	 */
	public List<User> getUserByGroup(String groupId){
		List<User> userList = new ArrayList<User>();
		userList = identityService.createUserQuery()
				.memberOfGroup(groupId)
				.list();
		return userList;
	}
	
	/**
	 * 根据分组名称获取该分组下所有的审批用户
	 * @param groupName 分组名称
	 * @return
	 */
	public List<User> getUserByGroupName(String groupName){
		Group group = identityService.createGroupQuery()
				.groupName(groupName)
				.singleResult();
		List<User> userList = new ArrayList<User>();
		userList = identityService.createUserQuery()
				.memberOfGroup(group.getId())
				.list();
		return userList;
	}
	
	/**
	 * 添加用户
	 * @param user
	 */
	public void addUser(@RequestBody UserEntityImpl user) {
		User userEntity = identityService.newUser(user.getId());
		userEntity.setFirstName(user.getFirstName());
		userEntity.setLastName(user.getLastName());
		userEntity.setEmail(user.getEmail());
		userEntity.setPassword(user.getPassword());
		identityService.saveUser(userEntity);
	}
	/**
	 * 根据当前节点获取下一节点的所有的审批人
	 * @param taskId 前台传来的待办任务的id
	 * @return
	 */
	public List<User> getNextUsers(String taskId){
		List<User> userList = new ArrayList<User>();
        //获取到运行时act_ru_identitylink中的数据
        List<IdentityLink> taskList = taskService.getIdentityLinksForTask(taskId);
        //遍历取出数据
        for (IdentityLink identityLink : taskList) {
            System.out.println(identityLink.getGroupId()+"==审批人名称:"+identityLink.getUserId()
                    +"==对应审批角色的ID"+identityLink.getTaskId());
            String groupId = identityLink.getGroupId();
            userList = identityService.createUserQuery()
    				.memberOfGroup(groupId)
    				.list();
        }
		return userList;
	}
	/**
	 * 获取所有的分组
	 * @return
	 */
	public List<Group> getAllGroup(){
		List<Group> groupList = new ArrayList<Group>();
		groupList = identityService.createGroupQuery().list();
		return groupList;
	}
	
	/**
	 * 添加分组
	 * @param group
	 */
	public void addGroup(GroupEntityImpl group) {
		Group groupEntiry = identityService.newGroup(group.getId());
		groupEntiry.setName(group.getName());
		groupEntiry.setType(group.getType());
		identityService.saveGroup(groupEntiry);
	}
	
	/**
	 * 添加用户和分组的关系
	 * @param groupId
	 * @param userId
	 */
	public void addRelateGroupUser(String groupId, String userId) {
		identityService.createMembership(userId, groupId);
	}
}
